/*******************************************************************************
* @Filename      : bsp_sys.h

* @Description   : bsp_sys.h

* @Version       : 1.0.0

* @Date          : 2023.12.2
*******************************************************************************/

#ifndef __BSP_SYS_H
#define __BSP_SYS_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"

/* Exported types ------------------------------------------------------------*/

/* Exported define -----------------------------------------------------------*/
#define BACKUP_PRIMASK()    uint32_t primask_bit=__get_PRIMASK()
#define RESTORE_PRIMASK()   __set_PRIMASK(primask_bit)
#define DISABLE_IRQ()       __disable_irq()
#define ENABLE_IRQ()        __enable_irq()

#define SYS_MS_TIM          TIM14
#define DelayMs(v)          BSP_SYS_MS_Delay(v)
#define GetMsTick()         BSP_SYS_GET_MS_Tick()

#define GetUsTick()         BSP_SYS_GET_US_Tick()
#define DelayUs(v)          BSP_SYS_US_Delay(v)


/* Exported constants --------------------------------------------------------*/

/* Exported variables --------------------------------------------------------*/
extern __IO uint32_t BSP_SYS_Tick;
extern __IO uint32_t BSP_SYS_1MS_Flag;
extern __IO uint32_t BSP_SYS_2MS_Tick;
extern __IO uint32_t BSP_SYS_2MS_Flag;
extern __IO uint32_t BSP_SYS_40MS_Tick;
extern __IO uint32_t BSP_SYS_40MS_Flag;

extern __IO uint32_t BSP_SYS_US_Tick;

/* Exported functions --------------------------------------------------------*/
void BSP_SYS_Init(void);
void BSP_SYS_MS_Delay(uint32_t ms);
void TIM3_Configure(void);

INLINE uint32_t BSP_SYS_GET_MS_Tick(void)
{
    return BSP_SYS_Tick;
}

INLINE uint32_t BSP_SYS_GET_US_Tick(void)
{
    return BSP_SYS_US_Tick;
}

INLINE void BSP_SYS_MS_TIM_Suspend(void)
{
    BSP_SYS_Tick = 0;
    BSP_SYS_2MS_Tick = 0;
    BSP_SYS_2MS_Flag = 0;
    BSP_SYS_40MS_Tick = 0;
    BSP_SYS_40MS_Flag = 0;
    TIM_ITConfig(SYS_MS_TIM, TIM_IT_Update, DISABLE);
    TIM_Cmd(SYS_MS_TIM, DISABLE);
}

INLINE void BSP_SYS_MS_TIM_Resume(void)
{
    TIM_ITConfig(SYS_MS_TIM, TIM_IT_Update, ENABLE);
    TIM_Cmd(SYS_MS_TIM, ENABLE);
}

#ifdef __cplusplus
}
#endif
#endif

/* End of File ---------------------------------------------------------------*/
